package com.logistics.pick.entity.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.logistics.erp.entity.domain.ErpInvReq;
import com.logistics.inventory.entity.model.UpdStation;
import com.logistics.material.entity.model.MaterialBackRow;
import com.logistics.material.entity.model.MaterialBackSave;
import com.logistics.material.entity.model.MaterialPickSave;
import com.logistics.pick.entity.model.PickDetailSave;
import com.logistics.utils.bean.UserBean;
import com.logistics.utils.tool.DateUtils;
import com.logistics.utils.tool.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 拣配明细
 * </p>
 *
 * @author yunjian.ban
 * @since 2024-06-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("lgs_pick_detail")
public class PickDetail implements Serializable {

    private static final long serialVersionUID = 1L;

    // id
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    // 拣配单
    private Integer pick;

    // 流向表id
    private Integer invbill;

    // 托盘
    private String container;

    //序号
    private Integer itemSeqNo;

    // 物料编码
    private String itemCode;

    // 物料任务号
    private String itemWorkcode;

    // 工序编号
    private String itemOpNo;

    // 物料名称
    private String itemName;

    // 物料单位
    private String itemUnit;

    // 数量
    private BigDecimal itemQty;

    // 拣配数量
    private BigDecimal qty;

    //后工序开工时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date nextStartDate;

    //物料维度，展示班组流向
    @TableField(exist = false)
    private String wcCode;

    //物料维度，展示班组流向
    @TableField(exist = false)
    private String wcName;

    @TableField(exist = false)
    private String erpInvBillCode;

    // 创建人组织编码
    private String createOrgCode;

    // 创建人部门编码
    private String createDepCode;

    // 创建人
    private String creator;

    // 创建时间
    private Date createTime;

    // 修改人
    private String updater;

    // 修改时间
    private Date updateTime;

    public PickDetail(){

    }

    public PickDetail(PickDetailSave save){
        this.id = StringUtils.isNotNull(save.getId()) ? Integer.parseInt(save.getId()) : null;
        this.pick = StringUtils.isNotNull(save.getPick()) ? Integer.parseInt(save.getPick()) : null;
        this.container = save.getContainer();
        this.itemCode = save.getItemCode();
        this.itemWorkcode = save.getItemWorkcode();
        this.itemOpNo = save.getItemOpNo();
        this.itemSeqNo = StringUtils.isNotNull(save.getItemSeqNo()) ? Integer.parseInt(save.getItemSeqNo()) : null;
        this.itemName = save.getItemName();
        this.itemUnit = save.getItemUnit();
        this.itemQty = StringUtils.isNotNull(save.getItemQty()) ? new BigDecimal(save.getItemQty()) : null;
        this.qty = StringUtils.isNotNull(save.getItemQty()) ? new BigDecimal(save.getItemQty()) : null;
        this.createOrgCode = save.getCreateOrgCode();
        this.createDepCode = save.getCreateDepCode();
        this.creator = save.getCreator();
        this.createTime = new Date();
        this.updater = save.getUpdater();
        this.updateTime = new Date();
    }

    public static PickDetail build(PickDetailSave save){
        return new PickDetail(save);
    }

    public static PickDetail build(UpdStation updStation){
        PickDetail pickDetail = new PickDetail();
        pickDetail.setContainer(updStation.getContainerCode());
        pickDetail.setUpdater(updStation.getUpdater());
        pickDetail.setUpdateTime(DateUtils.stringToDateFormat(updStation.getUpdateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        return pickDetail;
    }

    public static PickDetail build(String qty){
        PickDetail save = new PickDetail();
        save.setItemQty(new BigDecimal(qty));
        return save;
    }

    public static PickDetail build(MaterialPickSave save, Integer invBill){
        PickDetail pickDetail = new PickDetail();
        pickDetail.setPick(StringUtils.isNotNull(save.getPickId()) ? Integer.parseInt(save.getPickId()) : null);
        pickDetail.setContainer(save.getContainerCode());
        pickDetail.setInvbill(invBill);
        pickDetail.setItemCode(save.getItemCode());
        pickDetail.setItemOpNo(save.getItemOpNo());
        pickDetail.setItemName(save.getItemName());
        pickDetail.setItemUnit(save.getItemUnit());
        pickDetail.setItemSeqNo(StringUtils.isNotNull(save.getItemSeqNo()) ? Integer.parseInt(save.getItemSeqNo()) : null);
        pickDetail.setItemWorkcode(save.getTaskNo());
        pickDetail.setItemQty(StringUtils.isNotNull(save.getItemQty()) ? new BigDecimal(save.getItemQty()) : null);
        pickDetail.setQty(StringUtils.isNotNull(save.getItemQty()) ? new BigDecimal(save.getItemQty()) : null);
        pickDetail.setNextStartDate(DateUtils.stringToDateFormat(save.getNextStartDate(), DateUtils.YYYY_MM_DD_SLASH));
        pickDetail.setCreateOrgCode(save.getCreateOrgCode());
        pickDetail.setCreateDepCode(save.getCreateDepCode());
        pickDetail.setCreator(save.getCreator());
        pickDetail.setCreateTime(DateUtils.stringToDateFormat(save.getCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        pickDetail.setUpdater(save.getUpdater());
        pickDetail.setUpdateTime(DateUtils.stringToDateFormat(save.getUpdateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        pickDetail.setCreateOrgCode(save.getCreateOrgCode());
        return pickDetail;
    }

    public static PickDetail build(MaterialBackSave save, MaterialBackRow row, Integer pick){
        PickDetail pickDetail = new PickDetail();
        pickDetail.setPick(pick);
        pickDetail.setContainer(save.getContainer());
        pickDetail.setItemCode(row.getItemCode());
        pickDetail.setItemOpNo(row.getSeqNo());
        pickDetail.setItemName(row.getItemName());
        pickDetail.setItemUnit(row.getItemUnit());
//        pickDetail.setItemSeqNo(StringUtils.isNotNull(save.getItemSeqNo()) ? Integer.parseInt(save.getItemSeqNo()) : null);
        pickDetail.setItemWorkcode(row.getItemWorkCode());
        pickDetail.setItemQty(StringUtils.isNotNull(row.getItemQty()) ? row.getItemQty() : null);
        pickDetail.setQty(StringUtils.isNotNull(row.getItemQty()) ? row.getItemQty() : null);
        pickDetail.setCreateOrgCode(save.getCreateOrgCode());
        pickDetail.setCreateDepCode(save.getCreateDepCode());
        pickDetail.setCreator(save.getCreator());
        pickDetail.setCreateTime(DateUtils.stringToDateFormat(save.getCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        pickDetail.setUpdater(save.getUpdater());
        pickDetail.setUpdateTime(DateUtils.stringToDateFormat(save.getUpdateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        pickDetail.setCreateOrgCode(save.getCreateOrgCode());
        return pickDetail;
    }
}
